package nickthissen.iracing.activities;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.view.MenuItem;

import nickthissen.iracing.iracingforum2.R;

public abstract class PostBaseActivity extends TaskActivity
{
	protected EditText txtTitle, txtUser, txtContent;
	protected ImageButton btnFindUser;

	protected int type;

    private static final String TYPE = "TYPE";

	public static final int TYPE_POST = 0;
	public static final int TYPE_QUOTE = 1;
	public static final int TYPE_EDIT = 2;
	public static final int TYPE_REPLYPM = 3;
	public static final int TYPE_SENDPM = 4;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        setContentView(R.layout.newthread);
        super.onCreate(savedInstanceState);
        
        txtTitle = (EditText) this.findViewById(R.id.txtTitle);
        txtUser = (EditText) this.findViewById(R.id.txtUser);
        txtContent = (EditText) this.findViewById(R.id.txtContent);
        btnFindUser = (ImageButton) this.findViewById(R.id.btnFindUser);

        Bundle extras = savedInstanceState;
        if (extras == null)
        {
            Intent i = this.getIntent();
            extras = i.getExtras();
        }
        type = extras.getInt(TYPE);
    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu)
    {
        getSupportMenuInflater().inflate(R.menu.post, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId()) {
            case R.id.mnuPost:
                this.checkPost();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

	@Override
	public void onSaveInstanceState(Bundle outState)
	{
		outState.putInt(TYPE, type);
		super.onSaveInstanceState(outState);
	}

//	@Override
//	public void onRestoreInstanceState(Bundle state)
//	{
//		type = state.getInt(TYPE);
//		super.onRestoreInstanceState(state);
//	}

    protected abstract void onPostButtonClicked();

	private void checkPost()
	{
		if (TextUtils.isEmpty(txtContent.getText()))
		{
			Toast.makeText(this, "Content cannot be empty!", Toast.LENGTH_LONG);
		}
		else		
		{
			this.onPostButtonClicked();
		}
	}

    protected void postFinished()
    {
        this.setResult(RESULT_OK);
        this.finish();
    }
}
